package com.xiaoshuidi.cloud.module.contract.controller.admin.approval;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.approval.vo.*;
import com.xiaoshuidi.cloud.module.contract.convert.approval.ContractApprovalConfigConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.approval.ContractApprovalConfigDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.approval.ApprovalConfigService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;

import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 合同账单审批配置")
@RestController
@RequestMapping(ApiConstants.NAME + "/approval-config")
@Validated
public class ContractApprovalConfigController {

    @Resource
    private ApprovalConfigService approvalConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建合同账单审批配置")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:create')")
    public CommonResult createApprovalConfig(@Valid @RequestBody ContractApprovalConfigReqVO req) {
        approvalConfigService.createApprovalConfig(req);
        return success();
    }

    @PostMapping("/update")
    @Operation(summary = "更新合同账单审批配置")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:update')")
    public CommonResult<Boolean> updateApprovalConfig(@Valid @RequestBody ContractApprovalConfigReqVO updateReqVO) {
        approvalConfigService.updateApprovalConfig(updateReqVO);
        return success(true);
    }

    @GetMapping ("/delete")
    @Operation(summary = "删除合同账单审批配置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:delete')")
    public CommonResult<Boolean> deleteApprovalConfig(@RequestParam("id") Long id) {
        approvalConfigService.deleteApprovalConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同账单审批配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:query')")
    public CommonResult<ContractApprovalConfigRespVO> getApprovalConfig(@RequestParam("id") Long id) {
        ContractApprovalConfigDO approvalConfig = approvalConfigService.getApprovalConfig(id);
        return success(ContractApprovalConfigConvert.INSTANCE.convert(approvalConfig));
    }

    @GetMapping("/list")
    @Operation(summary = "获得合同账单审批配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:query')")
    public CommonResult<List<ContractApprovalConfigRespVO>> getApprovalConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<ContractApprovalConfigDO> list = approvalConfigService.getApprovalConfigList(ids);
        return success(ContractApprovalConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同账单审批配置分页")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:query')")
    public CommonResult<PageResult<ContractApprovalConfigRespVO>> getApprovalConfigPage(@Valid ContractApprovalConfigPageReqVO pageVO) {

        PageResult<ContractApprovalConfigDO> pageResult = approvalConfigService.getApprovalConfigPage(pageVO);
        return success(ContractApprovalConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同账单审批配置 Excel")
//    @PreAuthorize("@ss.hasPermission('contract:approval-config:export')")
    @OperateLog(type = EXPORT)
    public void exportApprovalConfigExcel(@Valid ContractApprovalConfigExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ContractApprovalConfigDO> list = approvalConfigService.getApprovalConfigList(exportReqVO);
        // 导出 Excel
        List<ContractApprovalConfigExcelVO> datas = ContractApprovalConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "合同账单审批配置.xls", "数据", ContractApprovalConfigExcelVO.class, datas);
    }

}
